refactor(pkg/utils): add unified atomic file write utility#706
refactor(pkg/utils): add unified atomic file write utility#706mengzhuo merged 12 commits intosipeed:mainfrom
Conversation
pkg/utils/file.go
Outdated
| // | ||
| // Copyright (c) 2026 PicoClaw contributors | ||
|
|
||
| package utils |
There was a problem hiding this comment.
I think we need a better package name instead of utils
| // | ||
| // // Public readable file | ||
| // err := utils.WriteFileAtomic("public.txt", data, 0o644) | ||
| func WriteFileAtomic(path string, data []byte, perm os.FileMode) error { |
There was a problem hiding this comment.
The way you implement "atomicwrite" just using tmpfile for the job, it will corrupt the file if write to tmp file failed too.
There was a problem hiding this comment.
You're correct. I've fixed the issue。
|
Please run |
Done — I ran golangci-lint with --fix and pushed the follow-up commit. The import-order/gci issues are resolved on |
|
@mosir Unifying the atomic write pattern across 9 locations into one utility is a solid refactor. Adding the explicit Sync() call before rename is especially important for flash storage on edge devices where power loss is a real concern. The before/after metrics in the PR description are a nice touch. We have a PicoClaw Dev Group on Discord for contributors to chat and collaborate. If you're interested, send an email to |
Summary
This PR introduces a unified atomic file write utility (
WriteFileAtomic) that eliminates code duplicationacross 9 locations while improving data reliability on low-power edge devices.
Problem
The existing atomic write pattern (temp file + rename) was missing explicit
Sync()calls in multiplelocations. On flash storage with write caching (SD cards, eMMC, SPI flash), this could lead to data corruption
on sudden power loss:
Solution
Created
pkg/utils/file.gowithWriteFileAtomic()function that:Sync()before rename to ensure data is physically writtenChanges
New file:
pkg/utils/file.go- Unified atomic write utility (97 lines)Refactored files (all now use
utils.WriteFileAtomic):pkg/tools/filesystem.go- hostFs, sandboxFspkg/config/config.go- SaveConfigpkg/auth/store.go- SaveStorepkg/cron/service.go- saveStoreUnsafepkg/agent/memory.go- WriteLongTerm, AppendTodaypkg/heartbeat/service.go- HEARTBEAT.md creation (0o644, user-editable)pkg/skills/installer.go- skill installationpkg/tools/skills_install.go- skill metadatapkg/state/state.go- saveAtomicBenefits
Testing
All existing tests pass:
est ./pkg/tools/... ./pkg/config/... ./pkg/auth/... ./pkg/cron/... ./pkg/agent/... ./pkg/state/...
g/utils/...
Note: Some tests fail on Windows due to pre-existing platform-specific permission handling issues (unrelated
to this PR).
Security Notes
HEARTBEAT.mduses 0o644 intentionally as it's a user-editable config file without sensitivedata
Related Issues
Fixes potential data corruption on power loss for edge devices running PicoClaw on flash storage (SD cards,
eMMC, SPI flash).